package com.hbmcc.heshenghuo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hbmcc.heshenghuo.model.http.bean.TitleAndUrlBean;
import com.hbmcc.heshenghuo.util.DensityUtil;
import com.zhihaoliang.busEncy.R;

/**
 * Copyright (c) 2020
 * 欣网互联网络科技有限公司
 * author: whs
 * created on: 2020/4/29 9:06
 * description:
 */
public class FloorTitle extends RelativeLayout {
    /**
     * 初始化是否显示 默认显示
     */
    private boolean isShow = true;
    /**
     * 是否居中  默认居左
     */
    private boolean isCenter = false;
    /**
     * 是否显示更多 默认不显示
     */
    private boolean isShowMore = false;
    /**
     * 设置标题字体 是否是粗体。默认粗体
     */
    private boolean titleIsBold = true;

    /**
     * 设置主标题字号
     */
    private float titleSize = 20;

    /**
     * 设置副标题字号
     */
    private float subtitleSize = 11;

    private TextView title, subtitle, more;
    private String titleText;
    private RelativeLayout main;
    private String moreUrl = "";

    private onMoreClickListener onMoreClickListener = null;

    public FloorTitle(Context context) {
        super(context);
    }

    public FloorTitle(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context, attributeSet);
    }

    public FloorTitle(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        init(context, attributeSet);
    }

    public void init(Context context, AttributeSet attributeSet) {
        LayoutInflater.from(context).inflate(R.layout.floor_title, this);
        title = findViewById(R.id.title);
        subtitle = findViewById(R.id.subtitle);
        more = findViewById(R.id.more);
        main = findViewById(R.id.main);

        TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.FoundFloorTitle);
        setTitleText(a.getString(R.styleable.FoundFloorTitle_titleText));
        setTitleSize(a.getDimension(R.styleable.FoundFloorTitle_titleTextSize, titleSize));
        setSubtitleText(a.getString(R.styleable.FoundFloorTitle_subtitleText));
        setSubtitleSize(a.getDimension(R.styleable.FoundFloorTitle_subtitleTextSize, subtitleSize));
        setShow(a.getBoolean(R.styleable.FoundFloorTitle_isShow, isShow));
        setCenter(a.getBoolean(R.styleable.FoundFloorTitle_isCenter, isCenter));
        setShowMore(a.getBoolean(R.styleable.FoundFloorTitle_isShowMore, isShowMore));
        setTitleIsBold(a.getBoolean(R.styleable.FoundFloorTitle_titleIsBold, titleIsBold));
        a.recycle();

        //更多点击事件
        more.setOnClickListener(view -> {
            if (!TextUtils.isEmpty(moreUrl)) {
                //PageManager.build(moreUrl).go(context);
                if (null != onMoreClickListener) {
                    onMoreClickListener.onClick(moreUrl);
                }
            }
        });
    }

    /**
     * 点击回调
     */
    public interface onMoreClickListener {
        void onClick(String url);
    }

    public void setOnMoreClickListener(onMoreClickListener onMoreClickListener) {
        this.onMoreClickListener = onMoreClickListener;
    }

    public void setShow(boolean show) {
        isShow = show;
        if (show) {
            main.setVisibility(VISIBLE);
        } else {
            main.setVisibility(GONE);
        }
    }


    public void setCenter(boolean center) {
        isCenter = center;
        if (center) {
            //隐藏副标题
            subtitle.setVisibility(GONE);
            LayoutParams layoutParams =
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            title.setLayoutParams(layoutParams);
        } else {
            subtitle.setVisibility(VISIBLE);
            LayoutParams layoutParams =
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            title.setLayoutParams(layoutParams);
        }
    }

    public void setShowMore(boolean showMore) {
        isShowMore = showMore;
        if (showMore) {
            more.setVisibility(VISIBLE);
        } else {
            more.setVisibility(GONE);
        }
    }

    public void setTitleText(String titleText) {
        if (titleText != null) {
            this.titleText = titleText;
            title.setText(titleText);
        }
    }

    public void setSubtitleText(String subtitleText) {
        if (!isCenter) {
            if (subtitleText != null) {
                subtitle.setText(subtitleText);
            }
        }
    }

    public void setTitleIsBold(boolean titleIsBold) {
        this.titleIsBold = titleIsBold;
        title.getPaint().setFakeBoldText(titleIsBold);
    }

    public void setHeight(int height) {
        LayoutParams params = (LayoutParams) main.getLayoutParams();
        params.height = DensityUtil.dip2px(getContext(), height);
        main.setLayoutParams(params);
    }

    public void setHeight(int height, int marginTop) {
        LayoutParams params = (LayoutParams) main.getLayoutParams();
        params.height = DensityUtil.dip2px(getContext(), height);
        params.topMargin = DensityUtil.dip2px(getContext(), marginTop);
        main.setLayoutParams(params);
    }

    public void setMoreUrl(String moreUrl) {
        this.moreUrl = moreUrl;
    }

    public void setTitleSize(float titleSize) {
        if (titleText != null) {
            this.titleSize = titleSize;
            title.setTextSize(titleSize);
        }

    }

    public void setSubtitleSize(float subtitleSize) {
        if (subtitle != null) {
            this.subtitleSize = subtitleSize;
            subtitle.setTextSize(subtitleSize);
        }
    }


    /**
     * 传入titleBean
     */
    public void setBean(TitleAndUrlBean bean) {
        if (null == bean) {
            setShow(false);
            return;
        }

        //是否显示
        setShow("1".equals(bean.getF_IS_TITLE()));

        //是否居中
        setCenter("1".equals(bean.getF_FLOOR_SEAT()));

        //设置主标题
        setTitleText(bean.getF_NAME());
        //设置副标题
        setSubtitleText(bean.getF_FU_TITLE());
        //设置更多
        if (!TextUtils.isEmpty(bean.getF_MORE_NAME())) {
            more.setText(bean.getF_MORE_NAME());
        }
        //是否显示更多
        if (!TextUtils.isEmpty(bean.getF_MORE_URL())) {
            setShowMore(true);
            //设置更多URL
            setMoreUrl(bean.getF_MORE_URL());
        } else {
            setShowMore(false);
        }
    }
}
